<?php $this->pageTitle = 'Commissions Payable'; ?>
<?php Yii::app()->clientScript->registerScriptFile('/js/jquery.cookie/jquery.cookie.js'); ?>
<?php
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
	$('.search-form').toggle();
	return false;
});
");
?>
<div class="main-title no-border clearfix">
	<h1 class="float-left">Commissions Payable</h1>
	<div class="controls">
		<a id="addPaymentVoucher" class="link-button with-icon" href="<?php echo $this->createUrl('commissionPayable/create'); ?>">
			<img src="/images/icons/fugue/document--plus.png" />
			Create Payable Item
		</a>
		<?php if (!is_null($payableItem->payment_id)): ?>
			<a id="back-to-receipt" class="link-button with-icon" href="<?php echo $this->createUrl('receipt/view', array('id' => $payableItem->payment_id)); ?>">
				<img src="/images/icons/fugue/arrow-curve-180-left.png" />
				Back to Receipt
			</a>
		<?php endif; ?>
	</div>
</div>
<div class="flat_area">
	<div class="grid_16 relative">			
		<div class="search-form">
			<?php
			$this->renderPartial('_search', array(
				'payableItem' => $payableItem
			));
			?>
		</div><!-- search-form -->
		<?php
		$form = $this->beginWidget('GxActiveForm', array(
			'id' => 'bulk-action-form',
			'action' => array('paymentVoucher/bulkCreate'),
				));
		?>
		<div id="ps-payable-items-summary-index">
			<h4 class="align-right"><i>* denotes amount has been adjusted</i></h4>
		
			<?php
			$this->widget('PsGridView', array(
				'id' => 'commissions-payable-index-grid',
				'dataProvider' => $dataProvider,
				'template' => '{items}{pager}{pagesize}',
//						'mergeColumns' => array('user.id', 'user.name'),
//						'extraRowColumns' => array('user.id', 'user.name'),
				'htmlOptions' => array(
					'class' => 'grid-view grey'
				),
				'itemsCssClass' => 'items fixed',
				'pagerCssClass' => 'pager float-right',
				'pagesizeCssClass' => 'pager-pagesize float-right',
				'columns' => array(
					'id' => array(
						'class' => 'CCheckBoxColumn',
						'name' => 'id',
						'value' => '$data->id',
						'selectableRows' => 2,
						'disabled' => '$data->comm_type == PayableItem::COMM_TYPE_ADJUSTMENT',
						'cssClassExpression' => '$data->comm_type == PayableItem::COMM_TYPE_ADJUSTMENT ? "a" : "b"',
						'checkBoxHtmlOptions' => array(
							'name' => 'payable_item_id[]',
						),
						'htmlOptions' => array(
							'width' => 16,
						),
					),
					array(
						'header' => '#',
						'name' => 'id',
						'type' => 'raw',
						'value' => '"#" . $data->id',
						'value' => 'CHtml::link("#" . $data->id, array("view", "id" => $data->id), array("class" => "blue link"))',
						'headerHtmlOptions' => array(
							'width' => 50
						),
					),
					array(
						'header' => 'Date',
						'name' => 'create_time',
						'value' => 'Yii::app()->dateFormatter->formatDateTime(strtotime($data->create_time), "short", null)',
						'headerHtmlOptions' => array(
							'width' => 80
						)
					),
					array(
						'header' => 'Payee Name',
						'name' => 'party.user.name',
						'type' => 'raw',
						'value' => '$data->comm_type != PayableItem::COMM_TYPE_EXTERNAL ? CHtml::link($data->getPayeeName(), array("/manage/users/view", "id" => $data->payeeParty->getUser()->id), array("class" => "blue link")) : $data->getPayeeName()',
						'headerHtmlOptions' => array(
							'width' => '120'
						),
						'htmlOptions' => array('class' => 'no-wrap')
					),
					array(
						'header' => 'Receipt No.',
						'name' => 'payment.payment_number',
						'type' => 'raw',
						'value' => 'CHtml::link((isset($data->payment))?"#".$data->payment->payment_number:"", array("receipt/view", "id" => $data->payment->id), array("class" => "blue link"))',
						'headerHtmlOptions' => array(
							'width' => 80
						)
					),
					array(
						'header' => 'Transaction',
						'name' => 'propertyCase.property.prop_address_formatted',
						'type' => 'raw',
						'value' => 'CHtml::link($data->propertyCase->case_number, array("transaction/default", "id" => $data->propertyCase->id), array("class" => "blue link")) . "<br/>" . $data->propertyCase->property->prop_address_formatted',
						'headerHtmlOptions' => array(
							'width' => 250
						),
						'htmlOptions' => array('class' => 'no-wrap')
					),
					array(
						'header' => 'Category',
						'name' => 'comm_type',
						'value' => '$data->getCommTypeFormatted()',
						'headerHtmlOptions' => array(
							'width' => 40
						),
						'htmlOptions' => array(
							'class' => 'align-center')
					),
					array(
						'header' => 'Amount *',
						'name' => 'net_amount',
						'type' => 'raw',
						'value' => 'Yii::app()->numberFormatter->formatCurrency($data->getAdjustedAmount($data->payee_party_id), ($data->payment->currency_code)?$data->payment->currency_code:"$") . ($data->getTotalAdjustments() <> 0 ? "*" : "")',
						'htmlOptions' => array(
							'class' => 'align-right',
						),
						'headerHtmlOptions' => array(
							'class' => 'align-right',
							'width' => 100
						)
					),
//					array(
//						'header' => 'Adjusted',
//						'value' => '$data->getTotalAdjustments() <> 0 ? "Yes" : ""',
//						'htmlOptions' => array(
//							'class' => 'align-center',
//						),
//						'headerHtmlOptions' => array(
//							'class' => 'align-center',
//							'width' => 60
//						)
//					),
					array(
						'header' => 'Description',
						'value' => '$data->description ? $data->description : "-"',
						'headerHtmlOptions' => array(
							'width' => 180,
							'style' => 'padding-left: 10px;'
						),
						'htmlOptions' => array(
							'class' => 'no-wrap',
							'style' => 'padding-left: 10px;'
						)
					),
					array(
						'header' => 'Mode',
						'value' => '$data->is_manual ? "Manual" : "Auto"',
						'headerHtmlOptions' => array(
							'width' => 70,
							'class' => 'align-center',
						),
						'htmlOptions' => array(
							'class' => 'align-center',
						)
					),
					array(
						'header' => 'PV No.',
						'type' => 'raw',
						'value' => '$data->hasPaymentVoucher() ? CHtml::link("#" . $data->paymentVoucher->payment_number, array("paymentVoucher/view", "id" => $data->payment_voucher_id), array("class" => "blue link")) : "Not assigned"',
						'headerHtmlOptions' => array(
							'width' => 100
						)
					),
					'actions' => array(
						'header' => 'Action',
						'class' => 'CButtonColumn',
						'template' => '{update}&nbsp;&nbsp;{delete}',
						'buttons' => array(
							'update' => array(
								'label' => 'Edit',
								'url' => 'Yii::app()->createUrl("commissionPayable/update", array("id" => $data->id))',
								'visible' => '$data->is_manual'
							),
							'delete' => array(
								'label' => 'Delete',
								'visible' => '$data->is_manual',
								'url' => '',
								'click' => "js:prestige.commissionPayable.index.onActionDeleteClick"
							)
						),
						'htmlOptions' => array(
							'style' => 'width: 70px'
						)
					),
				),
			));
			?>
		</div>
		<div id="bulk-actions-form">
			<?php echo GxHtml::submitButton(Yii::t('app', 'Create payment vouchers with selected'), array('class' => 'blue', 'name' => 'bulk_action_confirm')); ?>
			<?php
			echo CHtml::dropDownList('bulk_action', '', array(
				'create_pv_user' => 'Grouped by selected users',
				'create_pv_item' => 'For each individual item',
			));
			?>
		</div>
		<?php $this->endWidget(); ?>
	</div>
</div>

<?php $this->renderPartial('_dialog_delete_payable_item', array()); ?>
